package app.Insam.VNC;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class InsamService extends Service {
	public static final String TAG = "InsamApp";
	
	static final int ACTION_SCAN_WIFI=0x1;
	static final int ACTION_SCAN_LOCATION=0x2;
	
	protected InsamScheduler mScheduler;
	protected InsamLocationManager mLocationManager;
	protected InsamWifiManager mWifiManager;
	protected InsamNotificationManager mNotiManager;
	protected Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message _msg) {
			switch(_msg.what) {
			case ACTION_SCAN_WIFI:
				mWifiManager.checkWifi();
				break;	
			case ACTION_SCAN_LOCATION:
				mLocationManager.checkLocation();	
				break;
			default:
				Log.e(TAG, "unauthorized signal");				
			}				
		}
	};	
			
	@Override
	public void onCreate() {
		InsamDebugger.toast(TAG, "Start Service");
//		Log.v(TAG, "create Service");
		
		mScheduler = new InsamScheduler(mHandler);
		mWifiManager = new InsamWifiManager(this, mScheduler);
		mLocationManager = new InsamLocationManager(this, mScheduler);
		mNotiManager = new InsamNotificationManager(this);

		super.onCreate();		
	}
	
	@Override
	public int onStartCommand(Intent _intent, int _flags, int _startId) {
//		Toast.makeText(this, "Service:onStartCommand", Toast.LENGTH_SHORT).show();
		int flag;
		if(_intent==null) {
			Log.v(TAG, "start command Service(with null)");
			flag=ACTION_SCAN_LOCATION;
		} else {
			flag=_intent.getFlags();
		}
		
		switch(flag) {
		case ACTION_SCAN_LOCATION:
			Log.v(TAG, "start command Service(with check Location)");
			mLocationManager.checkLocation();
			break;
		case ACTION_SCAN_WIFI:
			Log.v(TAG, "start command Service(with check Wifi)");
			mWifiManager.checkWifi();
			break;
		case InsamDebugger.ACTION_SHOW_DELAY: /* this is for debug */
			InsamDebugger.showDelay();
			break;
		default:
			Log.e(TAG, "unauthorized signal:" + flag);	
			Log.e(TAG, "call flag:" + _flags);
			Log.e(TAG, "startId:" + _startId);
			Log.e(TAG, "getAction:" + _intent.getAction());
			Log.e(TAG, "getScheme:" + _intent.getScheme());
			Log.e(TAG, "toString:" + _intent.toString());
			
			mLocationManager.checkLocation();
		}			
		return START_STICKY;
	}
	
	public void inOffice() {
		Log.i(TAG, "enter office");
		InsamApp.InOffice=true;
		startForeground(InsamNotificationManager.NOTI_OFFICE, mNotiManager.getNotiOffice());
	}
	
	public void outOffice() {
		Log.i(TAG, "leave Office");
		InsamApp.InOffice=false;
		stopForeground(true);	
	}
	
/*	public void notify(int _id, String _text) {		
	mNotiManager.notify(_id, _text);	
	}
*/
	
	@Override
	public void onDestroy() {
		InsamDebugger.toast(TAG, "Stop Service");
		//Log.d(TAG, "destroy Service");
		mScheduler.stop();
		mWifiManager.stop();
		mLocationManager.stop();
		// TODO 서비스가 꺼지지 못하게 하면 좋을텐데...(BR로 다시실행?)
		super.onDestroy();
	}

	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
	}
	
	@Override
	public IBinder onBind(Intent _intent) {
		// TODO Auto-generated method stub
		return null;
	}
}
